"""Lead note repository implementation (Synchronous)."""

from __future__ import annotations

from sqlalchemy.orm import Session, joinedload

from ..models.lead_note import LeadNote
from .base import Repository
from ..schemas.lead_note import LeadNoteCreate, LeadNoteUpdate


class LeadNoteRepository(Repository[LeadNote, LeadNoteCreate, LeadNoteUpdate]):
    """Repository for LeadNote operations (Synchronous)."""

    def __init__(self, session: Session) -> None:
        super().__init__(LeadNote, session)

    def get_by_lead(self, lead_id: int, skip: int = 0, limit: int = 50) -> list[LeadNote]:
        """Get notes for a specific lead with pagination."""
        return (
            self.session.query(LeadNote)
            .filter(LeadNote.lead_id == lead_id)
            .options(joinedload(LeadNote.author))
            .order_by(LeadNote.created_at.desc())
            .offset(skip)
            .limit(limit)
            .all()
        )

    def get_latest_note(self, lead_id: int) -> LeadNote | None:
        """Get the most recent note for a lead."""
        return (
            self.session.query(LeadNote)
            .filter(LeadNote.lead_id == lead_id)
            .options(joinedload(LeadNote.author))
            .order_by(LeadNote.created_at.desc())
            .first()
        )
